package dragon.social.core;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.social.security.SocialUserDetails;
import org.springframework.social.security.SocialUserDetailsService;
import org.springframework.stereotype.Component;

@Component
public class UserDetailsServiceImpl implements UserDetailsService, SocialUserDetailsService {

    Logger log = LoggerFactory.getLogger(UserDetailsServiceImpl.class);
    /**
     * 表单登录时，通过账号密码获取用户信息
     *
     * @param username
     * @return
     * @throws UsernameNotFoundException
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //到数据库查找，或者其他方式去查找，找到用户名和密码
        String password = "12345";
        if (username.equals("Leo")) {
            password = "qwer";
        }
        log.debug("获取user 信息");
        UserDetailsImpl userDetails = new UserDetailsImpl(username, password);
        return userDetails;
    }

    /**
     * 社交登录时，通过userid获取用户信息
     *
     * @param userId the user ID used to lookup the user details
     * @return the SocialUserDetails requested
     * @see UserDetailsService#loadUserByUsername(String)
     */
    @Override
    public SocialUserDetails loadUserByUserId(String userId) throws UsernameNotFoundException {
        //到数据库查找，或者其他方式去查找，找到用户名和密码
        String password = "12345";
        if (userId.equals("Leo")) {
            password = "qwer";
        }
        System.out.println("通过userId 获取user 信息");
        SocialUserDetails userDetails = new UserDetailsImpl("原罪", password,userId);
        return userDetails;
    }
}
